package com.example.lancooznbk.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.example.lancooznbk.R;

/**
 * Created by tangminglong on 17/9/21.
 * 自定义dialog
 */

public class CustomDialog extends Dialog {

    private  Context context;


    private Builder mBuilder;

    @Nullable
    private OnCustomClickListener clickPositive;
    @Nullable
    private OnCustomClickListener clicksNegative;


    public interface OnCustomClickListener {
         void onClick();
    }

    private CustomDialog(Builder builder) {
        super(builder.context, R.style.CustomDialog);
        mBuilder = builder;
        this.context = builder.context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        init();

        getWindow().setBackgroundDrawableResource(R.color.vifrification);
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_custom, null);
        setContentView(view);
        TextView tvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        tvTitle.setText(mBuilder.title);
        Drawable drawable = context.getResources().getDrawable(mBuilder.iconResId);
        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
        tvTitle.setCompoundDrawables(null,drawable,null,null);

        Button btnNegative = (Button) findViewById(R.id.btn_negative);
        btnNegative.setText(mBuilder.btnCancelText);
        btnNegative.setOnClickListener(new clickListener());

        Button btnPositive = (Button) findViewById(R.id.btn_positive);
        btnPositive.setOnClickListener(new clickListener());
        btnPositive.setText(mBuilder.btnConfirmText);

        setCanceledOnTouchOutside(mBuilder.cancelOnTouchOutside);
        clickPositive = mBuilder.listenerPositive;
        clicksNegative = mBuilder.listenerNegative;

    }


    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.btn_negative://取消退出
                    System.out.println("------long-------cancle cancle  cancle----------------");
                    if (clicksNegative != null) {
                        clicksNegative.onClick();
                    }
                    dismiss();
                    break;

                case R.id.btn_positive://确定退出
                    if (clickPositive != null) {
                        clickPositive.onClick();
                    }
                        dismiss();

                    break;

                default:
                    System.out.println("-----ooooooo11111defalt----------------");
                    break;
            }
        }

    }



     public static class Builder{
        private CharSequence title = "";//标题
        private OnCustomClickListener listenerPositive;//事件
         private OnCustomClickListener listenerNegative;//事件
        private Context context;
        private boolean cancelOnTouchOutside;
        private CharSequence btnCancelText;
        private CharSequence btnConfirmText;

        private @DrawableRes int iconResId;
         private CustomDialog instance;

         public Builder(Context context) {
            this.context = context;
        }

        public Builder title(CharSequence title){
            this.title = title;
            return this;
        }

         public Builder cancelOnTouchOutside(boolean cancelOnTouchOutside){
            this.cancelOnTouchOutside = cancelOnTouchOutside;
            return this;
        }


         public Builder negativeButton(CharSequence text ,OnCustomClickListener listenerNegative){
             this.btnCancelText = text;
             this.listenerNegative = listenerNegative;
             return this;
         }

         public Builder positiveButton(CharSequence text ,OnCustomClickListener listenerPositive){
            this.btnConfirmText = text;
            this.listenerPositive = listenerPositive;
            return this;
        }

         public Builder icon(@DrawableRes int iconResId){
            this.iconResId = iconResId;
            return this;
        }
         public CustomDialog build(){
            instance = new CustomDialog(this);
            return instance;
        }

         public void show(){
            if (instance == null){
                new RuntimeException("use build() before show()");
            }
            instance.show();
        }

    }

}